from api.handler.ce.AbstractBatchCE import AbstractBatchCE
from api.model.BatchOutputPipe import BatchOutputPipe
from api.model.FdInputPipe import FdInputPipe
from munch import Munch
import json
# from config.annotation import Value

class DangerLevelHandler(AbstractBatchCE):
    """学生分布情况"""
    def do_compute(self, output_stream: BatchOutputPipe, source_fds: FdInputPipe, params: dict):
        fd = source_fds.get_fd('scupersonmove')       # 宽表
        sql = "select locate as school_locate, area, deptname, statusname, count(personno) as person_num from (select distinct personno, area, deptname, statusname, locate from scupersonmove where (area is not null and deptname is not null and date>DATE_SUB(curdate(),INTERVAL 8 DAY))) as a group by locate, area, deptname, statusname having person_num >0"
        results = fd.query(sql)

        # raise AttributeError(str(results)[:200])

        level_fd = source_fds.get_fd('Community')     # 风险等级
        level_sql = "SELECT * FROM Community where DataTime = DATE_SUB(curdate(),INTERVAL 1 DAY)"

        level_results = level_fd.query(level_sql)

        output = {}     # 结果 {{key：v}，}
        # for level_result in level_results:
        #     output.update({level_result.CountyName: {}})

        for result in results:
            for level_result in level_results:
                if result.area == level_result.CountyName:
                    key = level_result.CountyName + str(level_result.DataTime) + result.deptname + '7'
                    if key in output.keys():
                        out_data = output[key]
                        if result.statusname == '学生':
                            out_data['student_num'] = result.person_num
                        if result.statusname == '教工':
                            out_data['teacher_num'] = result.person_num
                    else:
                        out_data = {'city': level_result.CityName,
                                    'county': level_result.CountyName,
                                    'countylat': level_result.CountyLat,
                                    'countylng': level_result.CountyLng,
                                    'date': level_result.DataTime,
                                    'days': 7,
                                    'dept': result.deptname,
                                    'key': key,
                                    'province': level_result.Province,
                                    'campus': result.school_locate,
                                    'level': level_result.Level}
                        if result.statusname == '学生':
                            out_data['student_num'] = result.person_num
                        if result.statusname == '教工':
                            out_data['teacher_num'] = result.person_num
                    output.update({key: out_data})
                    break

        self.task_logger.log(output)

        for item in output:
            values = output[item]
            if not values or not isinstance(values, dict):
                continue
            data = Munch()
            data.city = values.get('city')
            data.county = values.get('county')
            data.countylat = values.get('countylat')
            data.countylng = values.get('countylng')
            data.date = values.get('date')
            data.days = values.get('days')
            data.dept = values.get('dept')
            data.key = values.get('key')
            data.province = values.get('province')
            data.school_locate = values.get('campus')
            data.student_num = values.get('student_num') or 0
            data.teacher_num = values.get('teacher_num') or 0
            data.county_level = values.get('level')
            output_stream.output_for_upsert(data)